//
//  HWMineTopView.swift
//  HWanGame
//
//  Created by 王昌健 on 2021/12/22.
//  Copyright © 2021 94Wan. All rights reserved.
//

import UIKit

class HWMineTopView: UIView {
    
    /// 背景图
    var bgImageView:UIImageView!
    /// 头像
    var headImageView:UIImageView!
    /// 头像框
    var headFrameImageView:UIImageView!
    /// 个人信息
    var mineInfoButton:UIButton!
    /// 名字
    var nameLabel:GranibleLabel!
    /// 个人主页
    var userHomeButton:UIButton!
    /// 勋章
    var medalImageView:UIImageView!
    /// vip标记
    var vipTagImageView:UIImageView!
    /// 签到
    var signButton:UIButton!
    /// 卡券
    var cardNumLabel:UILabel!
    /// 卡券
    var cardTitleLabel:UILabel!
    /// 卡券
    var cardButton:UIButton!
    /// 钱包
    var walletNumLabel:UILabel!
    /// 钱包
    var walletTitleLabel:UILabel!
    /// 钱包
    var walletButton:UIButton!
    /// 我的游戏
    var myGameNumLabel:UILabel!
    /// 我的游戏
    var myGameTitleLabel:UILabel!
    /// 我的游戏
    var myGameButton:UIButton!
    /// 我的关注
    var myFollowNumLabel:UILabel!
    /// 我的关注
    var myFollowTitleLabel:UILabel!
    /// 我的关注
    var myFollowButton:UIButton!
    /// 开通vip
    var openVipButton:UIButton!
    /// 开通后展示
    var vipView:HWMineVipView!
    
    /// 消息
    var messageButton:UIButton!
    var badgeView:UIView!
    /// 设置
    var settingButton:UIButton!
    
    var showBadge:Bool? {
        didSet {
            if showBadge == true {
                badgeView.isHidden = false
            }else{
                badgeView.isHidden = true
            }
        }
    }
    var model:HWMineModel! {
        didSet {
//            bgImageView.sd_setImage(with: URL(string: model.config.bgimg), completed: nil)
//            headImageView.sd_setImage(with: URL(string: model.user.face), completed: nil)
//            headFrameImageView.sd_setImage(with: URL(string: model.user.head_frame), completed: nil)
//            if model.user.head_frame.count > 0{
//                headImageView.snp.remakeConstraints { make in
//                    make.left.equalTo(20*widthScale)
//                    if screenHeight < 812 {
//                        make.top.equalTo(60*widthScale)
//                    }else {
//                        make.top.equalTo(100*widthScale).priority(.low)
//                    }
//                    make.height.width.equalTo(62*widthScale)
//                }
//                nameLabel.snp.remakeConstraints { make in
//                    make.left.equalTo(headFrameImageView.snp.right).offset(12*widthScale)
//                    make.top.equalTo(headImageView.snp.top).offset(4*widthScale)
//                    make.right.equalTo(-12*widthScale)
//                }
//
//                cardNumLabel.snp.remakeConstraints { make in
//                    make.top.equalTo(headFrameImageView.snp.bottom).offset(10*widthScale)
//                    make.centerX.equalTo(cardTitleLabel)
//                }
//
//                cardTitleLabel.snp.remakeConstraints { make in
//                    make.left.equalTo(21*widthScale)
//                    make.top.equalTo(headFrameImageView.snp.bottom).offset(40*widthScale)
//                }
//
//                openVipButton.snp.remakeConstraints { make in
//                    make.left.equalTo(16*widthScale)
//                    make.top.equalTo(headFrameImageView.snp.bottom).offset(65*widthScale)
//                    make.right.equalTo(-16*widthScale)
//                    make.height.equalTo(66*widthScale)
//                }
//
//                bgImageView.snp.remakeConstraints { make in
//                    make.left.top.right.bottom.equalToSuperview()
//                    make.height.equalTo(380*widthScale)
//                }
//            }else{
//                headImageView.snp.remakeConstraints { make in
//                    make.left.equalTo(12*widthScale)
//                    if screenHeight < 812 {
//                        make.top.equalTo(60*widthScale)
//                    }else {
//                        make.top.equalTo(100*widthScale).priority(.high)
//                    }
//                    make.height.width.equalTo(62*widthScale)
//                }
//                nameLabel.snp.remakeConstraints { make in
//                    make.left.equalTo(headImageView.snp.right).offset(12*widthScale)
//                    make.top.equalTo(headImageView.snp.top).offset(4*widthScale)
//                    make.right.equalTo(-12*widthScale)
//                }
//
//                cardNumLabel.snp.remakeConstraints { make in
//                    make.top.equalTo(headImageView.snp.bottom).offset(10*widthScale)
//                    make.centerX.equalTo(cardTitleLabel)
//                }
//
//                cardTitleLabel.snp.remakeConstraints { make in
//                    make.left.equalTo(21*widthScale)
//                    make.top.equalTo(headImageView.snp.bottom).offset(40*widthScale)
//                }
//
//                openVipButton.snp.remakeConstraints { make in
//                    make.left.equalTo(16*widthScale)
//                    make.top.equalTo(headImageView.snp.bottom).offset(65*widthScale)
//                    make.right.equalTo(-16*widthScale)
//                    make.height.equalTo(66*widthScale)
//                }
//
//                bgImageView.snp.remakeConstraints { make in
//                    make.left.top.right.bottom.equalToSuperview()
//                    make.height.equalTo(361*widthScale)
//                }
//            }
//
//            nameLabel.text = model.user.user_name
//            if (model?.user.color.count ?? 0) > 1 {
//                var arr:[UIColor] = Array()
//                for color in model?.user.color ?? [] {
//                    arr.append(UIColor.colorWithHexString(hexString: color))
//                }
//                nameLabel.colors = arr
//            }else {
//                nameLabel.colors = [UIColor.colorWithHexString(hexString: model?.user.color[0] ?? "#FFFFFF"),UIColor.colorWithHexString(hexString: model?.user.color[0] ?? "#FFFFFF")]
//            }
//            if model.user.vip == "0"{
//                openVipButton.isHidden = false
//                vipView.isHidden = true
//                openVipButton.sd_setBackgroundImage(with: URL(string: model.config.huiyuan), for: .normal, completed: nil)
//            }else {
//                openVipButton.isHidden = true
//                vipView.isHidden = false
//                vipView.model = model
//
//            }
//
//            if model.user.is_qiandao {
//                signButton.setTitle("已签到", for: .normal)
//                signButton.setBackgroundImage(UIImage(named: "mineSignedBg"), for: .normal)
//                signButton.setTitleColor(.colorWithHexString(hexString: "#EEC46F"), for: .normal)
//            }else{
//                signButton.setTitle("签到", for: .normal)
//                signButton.setBackgroundImage(UIImage(named: "mineSignBg"), for: .normal)
//                signButton.setTitleColor(.colorWithHexString(hexString: "#444444"), for: .normal)
//            }
//
//            medalImageView.sd_setImage(with: URL(string: model.user.medal), completed: nil)
//            vipTagImageView.sd_setImage(with: URL(string: model.user.huiyuan_img), completed: nil)
//            cardNumLabel.text = model.user.coupon
//            walletNumLabel.text = model.user.point
//            myGameNumLabel.text = model.user.yuyue
//            myFollowNumLabel.text = model.user.coin
        }
    }
    /// 个人信息
    var mineInfoButtonClick:(()->())?
    /// 消息
    var messageButtonClick:(()->())?
    /// 设置
    var settingButtonClick:(()->())?
    /// 签到
    var signButtonClick:(()->())?
    /// 开通Vip
    var openVipButtonClick:(()->())?
    /// 卡券
    var cardButtonClick:(()->())?
    /// 钱包
    var walletButtonClick:(()->())?
    /// 我的游戏
    var myGameButtonClick:(()->())?
    /// 我的关注
    var myFollowButtonClick:(()->())?

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        initUI()
        
        initFrame()
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
    }

}
extension HWMineTopView {
    private func initUI() -> Void {
        bgImageView = UIImageView()
        bgImageView.backgroundColor = .clear
        addSubview(bgImageView)
        
        messageButton = UIButton(type: .custom)
        messageButton.setImage(UIImage(named: "mineMessageButton"), for: .normal)
        messageButton.addTarget(self, action: #selector(messageButtonClick(_:)), for: .touchUpInside)
        addSubview(messageButton)
        
        badgeView = UIView()
        badgeView.backgroundColor = .red
        badgeView.layer.cornerRadius = 3*widthScale
        addSubview(badgeView)
        
        settingButton = UIButton(type: .custom)
        settingButton.setImage(UIImage(named: "mineSettingButton"), for: .normal)
        settingButton.addTarget(self, action: #selector(settingButtonClick(_:)), for: .touchUpInside)
        addSubview(settingButton)
        
        headImageView = UIImageView()
        headImageView.layer.cornerRadius = 62/2 * widthScale
        headImageView.layer.borderColor = UIColor.white.cgColor
        headImageView.layer.borderWidth = 2*widthScale
        headImageView.layer.masksToBounds = true
        addSubview(headImageView)
        
        headFrameImageView = UIImageView()
        headFrameImageView.backgroundColor = .clear
        headFrameImageView.layer.masksToBounds = true
        headFrameImageView.contentMode = .scaleAspectFit
        addSubview(headFrameImageView)
        
        mineInfoButton = UIButton(type: .custom)
        mineInfoButton.addTarget(self, action: #selector(mineInfoButtonClick(_:)), for: .touchUpInside)
        addSubview(mineInfoButton)
        
        nameLabel = GranibleLabel()
        nameLabel.text = ""
        nameLabel.font = .boldSystemFont(ofSize: 18*widthScale)
        nameLabel.colors = [UIColor.colorWithHexString(hexString: "#FFFFFF")]
        addSubview(nameLabel)
        
        userHomeButton = UIButton(type: .custom)
        userHomeButton.setTitle("个人主页>", for: .normal)
        userHomeButton.setTitleColor(.colorWithHexString(hexString: "#CECECE"), for: .normal)
        userHomeButton.titleLabel?.font = .systemFont(ofSize: 12*widthScale)
        userHomeButton.addTarget(self, action: #selector(mineInfoButtonClick(_:)), for: .touchUpInside)
        addSubview(userHomeButton)
        
        medalImageView = UIImageView()
        addSubview(medalImageView)
        
        vipTagImageView = UIImageView()
        addSubview(vipTagImageView)
        
        signButton = UIButton(type: .custom)
        signButton.setTitle("签到", for: .normal)
        signButton.setTitleColor(.colorWithHexString(hexString: "#EEC46F"), for: .normal)
        signButton.titleLabel?.font = .systemFont(ofSize: 12*widthScale)
        signButton.addTarget(self, action: #selector(signButtonClick(_:)), for: .touchUpInside)
        addSubview(signButton)
        
        cardNumLabel = HWLabel.createBoldLabel(title: "0", color: "#FFFFFF", font: 16*widthScale)
        addSubview(cardNumLabel)
        
        cardTitleLabel = HWLabel.createLabel(title: "卡券", color: "#999999", font: 10*widthScale)
        addSubview(cardTitleLabel)
        
        cardButton = UIButton(type: .custom)
        cardButton.addTarget(self, action: #selector(cardButtonClick(_:)), for: .touchUpInside)
        addSubview(cardButton)
        
        walletNumLabel = HWLabel.createBoldLabel(title: "0", color: "#FFFFFF", font: 16*widthScale)
        addSubview(walletNumLabel)
        
        walletTitleLabel = HWLabel.createLabel(title: "积分", color: "#999999", font: 10*widthScale)
        addSubview(walletTitleLabel)
        
        walletButton = UIButton(type: .custom)
        walletButton.addTarget(self, action: #selector(walletButtonClick(_:)), for: .touchUpInside)
        addSubview(walletButton)
        
        myGameNumLabel = HWLabel.createBoldLabel(title: "0", color: "#FFFFFF", font: 16*widthScale)
        addSubview(myGameNumLabel)
        
        myGameTitleLabel = HWLabel.createLabel(title: "预约", color: "#999999", font: 10*widthScale)
        addSubview(myGameTitleLabel)
        
        myGameButton = UIButton(type: .custom)
        myGameButton.addTarget(self, action: #selector(myGameButtonClick(_:)), for: .touchUpInside)
        addSubview(myGameButton)
        
        myFollowNumLabel = HWLabel.createBoldLabel(title: "0", color: "#FFFFFF", font: 16*widthScale)
        addSubview(myFollowNumLabel)
        
        myFollowTitleLabel = HWLabel.createLabel(title: "平台币", color: "#999999", font: 10*widthScale)
        addSubview(myFollowTitleLabel)
        
        myFollowButton = UIButton(type: .custom)
        myFollowButton.addTarget(self, action: #selector(myFollowButtonClick(_:)), for: .touchUpInside)
        addSubview(myFollowButton)
        
        openVipButton = UIButton(type: .custom)
        openVipButton.addTarget(self, action: #selector(openVipButtonClick(_:)), for: .touchUpInside)
        addSubview(openVipButton)
        
        vipView = HWMineVipView()
        addSubview(vipView)
        
        cardNumLabel.setContentHuggingPriority(.required, for: .horizontal)
        walletNumLabel.setContentHuggingPriority(.required, for: .horizontal)
        myGameNumLabel.setContentHuggingPriority(.required, for: .horizontal)
        myFollowNumLabel.setContentHuggingPriority(.required, for: .horizontal)
        
        
        vipView.lookButtonClick = {
            self.openVipButtonClick?()
        }
    }
    
    private func initFrame() -> Void {
        bgImageView.snp.makeConstraints { make in
            make.left.top.right.bottom.equalToSuperview()
            make.height.equalTo(361*widthScale)
        }
        
        messageButton.snp.makeConstraints { make in
            if screenHeight < 812 {
                make.top.equalTo(40*widthScale)
            }else {
                make.top.equalTo(56*widthScale)
            }
            
            make.right.equalTo(settingButton.snp.left).offset(-16*widthScale)
            make.width.height.equalTo(20*widthScale)
        }
        
        badgeView.snp.makeConstraints { make in
            make.width.height.equalTo(6*widthScale)
            make.right.equalTo(messageButton.snp.right)
            make.top.equalTo(messageButton)
        }
        
        settingButton.snp.makeConstraints { make in
            make.right.equalTo(-12*widthScale)
            make.centerY.equalTo(messageButton)
            make.width.equalTo(21*widthScale)
            make.height.equalTo(19*widthScale)
        }
        
        headImageView.snp.makeConstraints { make in
            make.left.equalTo(12*widthScale)
            if screenHeight < 812 {
                make.top.equalTo(60*widthScale)
            }else {
                make.top.equalTo(100*widthScale)
            }
            make.height.width.equalTo(62*widthScale)
        }
        
        headFrameImageView.snp.makeConstraints { make in
            make.center.equalTo(headImageView)
            make.height.width.equalTo(90*widthScale)
        }
        
        mineInfoButton.snp.makeConstraints { make in
            make.edges.equalTo(headImageView)
        }
        
        nameLabel.snp.makeConstraints { make in
            make.left.equalTo(headImageView.snp.right).offset(12*widthScale)
            make.top.equalTo(headImageView.snp.top).offset(4*widthScale)
            make.right.equalTo(-12*widthScale)
        }
        
        userHomeButton.snp.makeConstraints { make in
            make.left.equalTo(nameLabel)
            make.top.equalTo(nameLabel.snp.bottom).offset(8*widthScale)
        }
        
        medalImageView.snp.makeConstraints { make in
            make.left.equalTo(userHomeButton.snp.right).offset(20*widthScale)
            make.centerY.equalTo(userHomeButton)
            make.width.height.equalTo(18*widthScale)
        }
        
        vipTagImageView.snp.makeConstraints { make in
            make.left.equalTo(medalImageView.snp.right).offset(7*widthScale)
            make.centerY.equalTo(medalImageView)
            make.width.height.equalTo(18*widthScale)
        }
        
        signButton.snp.makeConstraints { make in
            make.right.equalTo(-12*widthScale)
            make.top.equalTo(nameLabel.snp.top).offset(16*widthScale)
            make.height.equalTo(27*widthScale)
            make.width.equalTo(62*widthScale)
        }
        
        cardNumLabel.snp.makeConstraints { make in
            make.top.equalTo(headImageView.snp.bottom).offset(10*widthScale)
            make.centerX.equalTo(cardTitleLabel)
        }
        
        cardTitleLabel.snp.makeConstraints { make in
            make.left.equalTo(21*widthScale)
            make.top.equalTo(headImageView.snp.bottom).offset(40*widthScale)
        }
        
        cardButton.snp.makeConstraints { make in
            make.top.left.right.equalTo(cardNumLabel)
            make.bottom.equalTo(cardTitleLabel)
        }
        
        walletNumLabel.snp.makeConstraints { make in
            make.top.equalTo(cardNumLabel)
            make.centerX.equalTo(walletTitleLabel)
        }
        
        walletTitleLabel.snp.makeConstraints { make in
            make.top.equalTo(cardTitleLabel)
            make.left.equalTo(cardTitleLabel.snp.right).offset(78*widthScale)
        }
        
        walletButton.snp.makeConstraints { make in
            make.top.left.right.equalTo(walletNumLabel)
            make.bottom.equalTo(walletTitleLabel)
        }
        
        myGameNumLabel.snp.makeConstraints { make in
            make.top.equalTo(cardNumLabel)
            make.centerX.equalTo(myGameTitleLabel)
        }
        
        myGameTitleLabel.snp.makeConstraints { make in
            make.top.equalTo(cardTitleLabel)
            make.left.equalTo(walletTitleLabel.snp.right).offset(61*widthScale)
        }
        
        myGameButton.snp.makeConstraints { make in
            make.top.left.right.equalTo(myGameNumLabel)
            make.bottom.equalTo(myGameTitleLabel)
        }
        
        myFollowNumLabel.snp.makeConstraints { make in
            make.top.equalTo(cardNumLabel)
            make.centerX.equalTo(myFollowTitleLabel)
        }
        
        myFollowTitleLabel.snp.makeConstraints { make in
            make.top.equalTo(cardTitleLabel)
            make.left.equalTo(myGameTitleLabel.snp.right).offset(71*widthScale)
        }
        
        myFollowButton.snp.makeConstraints { make in
            make.top.left.right.equalTo(myFollowNumLabel)
            make.bottom.equalTo(myFollowTitleLabel)
        }
        
        openVipButton.snp.makeConstraints { make in
            make.left.equalTo(16*widthScale)
            make.top.equalTo(headImageView.snp.bottom).offset(65*widthScale)
            make.right.equalTo(-16*widthScale)
            make.height.equalTo(66*widthScale)
        }
        
        vipView.snp.makeConstraints { make in
            make.edges.equalTo(openVipButton)
        }

    }
}

extension HWMineTopView {
    @objc func messageButtonClick(_ button:UIButton) -> Void {
        self.messageButtonClick?()
    }
    
    @objc func settingButtonClick(_ button:UIButton) -> Void {
        self.settingButtonClick?()
    }
    
    @objc func signButtonClick(_ button:UIButton) -> Void {
        self.signButtonClick?()
    }
    
    @objc func openVipButtonClick(_ button:UIButton) -> Void {
        self.openVipButtonClick?()
    }
    
    @objc func mineInfoButtonClick(_ button:UIButton) -> Void {
        self.mineInfoButtonClick?()
    }
    
    @objc func cardButtonClick(_ button:UIButton) -> Void {
        self.cardButtonClick?()
    }
    
    @objc func walletButtonClick(_ button:UIButton) -> Void {
        self.walletButtonClick?()
    }
    
    @objc func myGameButtonClick(_ button:UIButton) -> Void {
        self.myGameButtonClick?()
    }
    
    @objc func myFollowButtonClick(_ button:UIButton) -> Void {
        self.myFollowButtonClick?()
    }
}
